package com.xxx.dt;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/15  14:04
 * @Copyright
 */
public class Test {
    /*
    * 多态（对象多态）：父类型指向子类对象
    * 多态的必备条件:
    * 1.继承（实现）
    * 2.重写
    *
    * 多态的好处：解耦
    *
    *多态的缺点：不能使用子类特有的内容
    *
    * */


    public static void main(String[] args) {
//      Student student = new Student();
//        student.eat();
//        student.study();

        //多态(对象多态)(方法多态)
//        Person student = new Student();//向上转型(自动类型提升)
//        student.eat();
        // student.study(); 无法再使用特有的功能

//        Student student1 = (Student) student;//向下转型
//        student1.study();

        Student student = new Student();
        show(student);

        Worker worker = new Worker();
        show(worker);

    }

    //解耦
    public static void show(Person person) {//多态
        person.eat();
        //ClassCastException 类型转换异常

        /*
            instanceof:复杂类型判断
        */
        if(person instanceof Student){

            Student student = (Student) person;
            student.study();
        }if (person instanceof Worker) {

            Worker worker = (Worker) person;
            worker.eat();
        }


    }

//    public static void show1(Person person) {
//        Worker worker = (Worker) Person;
//        worker.eat();
//        worker.woker();
//    }
}
